package com.tusuo.stds.ui.level;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelRecordList;
import com.tusuo.stds.util.Utils;

import cn.lemon.view.adapter.BaseViewHolder;
import cn.lemon.view.adapter.RecyclerAdapter;

public class LevelRecordRvAdapter extends RecyclerAdapter<LevelRecordList.LevelRecord> {

    private Context context;

    private OnItemClickListener onItemClickListener;

    public LevelRecordRvAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder<LevelRecordList.LevelRecord> onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        return new LevelRecordRvHolder(parent);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public class LevelRecordRvHolder extends BaseViewHolder<LevelRecordList.LevelRecord> {

        TextView tvTitle;
        TextView tvCreatedTime;
        TextView tvCorrectRate;
        TextView tvClearanceTime;
        ImageView ivStar1;
        ImageView ivStar2;
        ImageView ivStar3;

        public LevelRecordRvHolder(ViewGroup parent) {
            super(parent, R.layout.level_record_list_item);
        }

        @Override
        public void setData(LevelRecordList.LevelRecord data) {
            super.setData(data);

            tvTitle.setText(data.Title);
            tvCreatedTime.setText(context.getString(R.string.level_record_created_time, data.getFormattedCreateTime()));
            switch (data.CompletionStatus) {
                case 0:
                    ivStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 10:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 20:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_selected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
                case 30:
                    ivStar1.setImageResource(R.drawable.ic_star_selected);
                    ivStar2.setImageResource(R.drawable.ic_star_selected);
                    ivStar3.setImageResource(R.drawable.ic_star_selected);
                    break;
                case 100:
                default:
                    tvCreatedTime.setText(R.string.level_status_not_pass);
                    ivStar1.setImageResource(R.drawable.ic_star_unselected);
                    ivStar2.setImageResource(R.drawable.ic_star_unselected);
                    ivStar3.setImageResource(R.drawable.ic_star_unselected);
                    break;
            }

            tvCorrectRate.setText(context.getString(R.string.level_record_correct_rate, (int) (data.CorrectRate * 100)));
            tvClearanceTime.setText(context.getString(R.string.level_record_clearance_time,
                    Utils.secondsToHumanReadable(data.ClearanceTime)));

        }

        @Override
        public void onInitializeView() {
            super.onInitializeView();

            tvTitle = findViewById(R.id.level_record_list_item_tv_title);
            tvCreatedTime = findViewById(R.id.level_record_list_item_tv_created_time);
            tvCorrectRate = findViewById(R.id.level_record_list_item_tv_correct_rate);
            tvClearanceTime = findViewById(R.id.level_record_list_item_tv_clearance_time);
            ivStar1 = findViewById(R.id.level_record_list_item_iv_star_1);
            ivStar2 = findViewById(R.id.level_record_list_item_iv_star_2);
            ivStar3 = findViewById(R.id.level_record_list_item_iv_star_3);

        }

        @Override
        public void onItemViewClick(LevelRecordList.LevelRecord data) {
            super.onItemViewClick(data);

            if (onItemClickListener != null)
                onItemClickListener.onItemClick(data);
        }


    }


    interface OnItemClickListener {
        void onItemClick(LevelRecordList.LevelRecord data);
    }
}
